using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Deployment.Application;

namespace Curchy.PERTToolbox.WinUI
{
    public partial class About : Form
    {
        public About()
        {
            InitializeComponent();

            Assembly assembly = Assembly.GetEntryAssembly();
            lblVersion.Text = "Application v. " + Application.ProductVersion;
            if (ApplicationDeployment.IsNetworkDeployed)
                lblVersion.Text += " / Deployment v. " + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
            else
                lblVersion.Text += " / Not Network Deployed";
            lblProduct.Text = ((AssemblyProductAttribute)AssemblyProductAttribute.GetCustomAttribute(assembly, typeof(AssemblyProductAttribute))).Product;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void About_Load(object sender, EventArgs e)
        {
            Assembly entryAssembly = Assembly.GetEntryAssembly();

            AddAssembly(entryAssembly);

            // Initialize the source dir and Load all assembly
            string currentDir = Path.GetDirectoryName(entryAssembly.Location);
            foreach (string file in Directory.GetFiles(currentDir, "*.dll"))
            {
                AddAssembly(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(file)));
            }

            // Initialize the source dir and Load all assembly
            string commandLineDir = Path.Combine(Path.GetDirectoryName(entryAssembly.Location), "CommandLine");
            if (Directory.Exists(commandLineDir))
                foreach (string file in Directory.GetFiles(commandLineDir, "*.exe"))
                {
                    AddAssembly(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(file)));
                }

            // Initialize the plugins dir and Load all the plugins
            string pluginsDir = Path.Combine(Path.GetDirectoryName(entryAssembly.Location), "Plugins");
            if (Directory.Exists(pluginsDir))
                foreach (string file in Directory.GetFiles(pluginsDir))
                {
                    AddAssembly(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(file)));
                }

            tree.Sort();
        }

        private void AddAssembly(Assembly ass)
        {
            FileInfo fi = new FileInfo(ass.Location);

            TreeNode itm = new TreeNode();

            itm.Text = ass.GetName().Name;
            itm.Nodes.Add("version: " + ass.GetName().Version.ToString());
            itm.Nodes.Add("location: " + ass.Location);
            itm.Nodes.Add("creation date: " + fi.CreationTime.ToString());

            tree.Nodes.Add(itm);
        }
    }
}